package com.xjscrm.console.chat.mq.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.uzai.mobile.collect.api.dto.MicaTransDto;
import com.uzai.mobile.collect.api.dto.msg.MicaConversationAddMsg;
import com.uzai.mobile.collect.api.enums.MicaMsgType;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.enums.ChatConversationTypeEnum;
import com.xjscrm.console.chat.frame.dto.ChatSessionDTO;
import com.xjscrm.console.chat.mq.MsgHandler;
import com.xjscrm.console.dto.chat.ChatTalkSessionQueryDTO;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/11 18:14
 */
@Component
@Slf4j
public class ConversationAddMsgHandler implements MsgHandler<MicaConversationAddMsg> {

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;
    @Autowired
    private WwDeviceService wwDeviceService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public MicaMsgType msgType() {
        return MicaMsgType.CONVERSATION_ADD;
    }

    @Override
    public Class<MicaConversationAddMsg> msgClass() {
        return MicaConversationAddMsg.class;
    }

    @Override
    @Transactional
    public void doHandle(MicaConversationAddMsg micaFriendTalkMsg) {

        MicaTransDto micaTransDto = baseCache.get();

        Long merId = micaTransDto.getMerId();
        String deviceId = micaTransDto.getDeviceId();
        Integer deviceType = micaTransDto.getDeviceType();

        String conversationId = micaFriendTalkMsg.getGroupId();
        String conversationName = micaFriendTalkMsg.getGroupName();

        if (!ObjectUtils.allNotNull(conversationId, merId, deviceType) || StringUtils.isBlank(deviceId)) {
            log.error("MicaConversationAddMsg is illegal: {}", JSON.toJSONString(micaFriendTalkMsg));
            return;
        }
        //
        WwDevice wwDevice = wwDeviceService.getByDeviceId(merId, deviceId);
        if (Objects.isNull(wwDevice)) {
            log.error("MicaConversationAddMsg device is not found: {}", deviceId);
            return;
        }

        ChatTalkSessionQueryDTO query = new ChatTalkSessionQueryDTO();
        query.setMerId(merId);
        query.setDeviceUniqueId(wwDevice.getId());
        query.setConversationName(conversationName);
        query.setType(111);

        ChatTalkSession newTalkSession = new ChatTalkSession();
        // 新增回话, 如何不存在
        ChatSessionDTO talkSession = chatTalkSessionService.get(query);
        if (Objects.isNull(talkSession)) {
            talkSession = new ChatSessionDTO();

            newTalkSession.setId(IdUtil.getSnowflakeNextId());
            newTalkSession.setMerId(merId);
            newTalkSession.setDeviceId(deviceId);
            newTalkSession.setDeviceType(deviceType);
            newTalkSession.setDeviceUniqueId(wwDevice.getId());
            newTalkSession.setConversationId(conversationId);
            newTalkSession.setConversationName(conversationName);
            newTalkSession.setType(ChatConversationTypeEnum.group_conversation.getCode());

            newTalkSession.setOpenShield(0);

            newTalkSession.setCreateTime((int) (System.currentTimeMillis() / 1000L));
            newTalkSession.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

            chatTalkSessionService.add(newTalkSession);

        } else {
            newTalkSession.setId(talkSession.getId());
            newTalkSession.setMerId(merId);
            newTalkSession.setConversationId(conversationId);
            newTalkSession.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
            chatTalkSessionService.update(newTalkSession);
        }
        talkSession.setConversationId(conversationId);
    }
}
